﻿var tabArray = [false, false, false, false, false, false, false, false];

$(document).ready(function () {
	loadTab();
	$('#tab ul li').hover(arrowmove, arrowback); 
	for(var i = 0; i < $("div.search input, div.search select").length; i++)
	{
	    $($("div.search input")[i]).keypress(function(event){
            if ( event.which == 13 ) {
                event.preventDefault();
                if(this.id.indexOf("btnsearch") != -1)
                    search(this);
                else
                    search($(this).siblings('input[type="button"]')[0]);
            }
	    });
	}
	$('#tab ul li').click(function(e){
		e.preventDefault();
		changeTab($('#tab ul li').index($(this)));
	}); 
});

function changeTab(index)
{
	if($($('#tab ul li')[index]).hasClass('current') && $($('#tab_content .section')[index]).hasClass('current'))
		return;
		
	var current = $('#tab ul li').index($('#tab ul li.current'));
	
	$('#tab_content .section.current').fadeOut(0);
	$('#tab .current').toggleClass('current');
	$('#tab_content .current').toggleClass('current');
	
	$($('#tab_content .section')[index]).fadeIn(500);
	$($('#tab ul li')[index]).toggleClass('current');
	$($('#tab_content .section')[index]).toggleClass('current');

	if(tabArray[current] == false)
    {
        getProduct(current);
        clearSearchForm(current);
    }
}

function arrowmove()
{
	if($(this).hasClass('current'))
		return;
	var target = $('#tab ul li').index($(this));
	var pos = -8;
	for(var i = 0; i < target; i++)
	    pos += $($('#tab ul li')[i]).width();
	
	pos += $($('#tab ul li')[target]).width() / 2;
	
	$('#nav-arrow').animate({left: pos + 'px'}, {queue:false,duration:500});
}
function arrowback()
{
	if($(this).hasClass('current'))
		return;
	var target = $('#tab ul li').index($('#tab ul li.current'));
	
	var pos = -8;
	for(var i = 0; i < target; i++)
	    pos += $($('#tab ul li')[i]).width();
	    
	pos += $($('#tab ul li')[target]).width() / 2;
	
	$('#nav-arrow').animate({left: pos + 'px'}, {queue:false,duration:500});
}

function loadTab(){
    var id = "#content1";
    if($(id).length == 0)
        return;
    $(id)[0].innerHTML = "<center><img src='" + base_url + "/images/loading.gif'/></center>";
    $.post(base_url + "/ajax/ListProduct.aspx?mode=1", function(data){
        $(id)[0].innerHTML = data;
        $($(id)[0]).css('display', 'none');
        $($(id)[0]).fadeIn();
        tabArray[0] = true;
        loadRemainTabs();
    });
}
function loadRemainTabs(){
    for(var i = 2; i < 8; i++)
    {
        getProduct(i);
    }
}
function getProduct(mode)
{
    var id = "#content" + mode;
    $(id)[0].innerHTML = "<center><img src='" + base_url + "/images/loading.gif'/></center>";
    $.post(base_url + "/ajax/ListProduct.aspx?mode=" + mode, function(data){
        $(id)[0].innerHTML = data;
        $($(id)[0]).css('display', 'none');
        $($(id)[0]).fadeIn();
        tabArray[mode] = true;
    });
}
function search(control)
{
    var mode = control.id.replace("btnsearch", "");
    var name = $('#txtname' + mode).val();
    var supplier = $('#ddlbrand' + mode).val();
    var pfrom = $('#ddlpricefrom' + mode).val();
    var pto = $('#ddlpriceto' + mode).val();
    
    $("#content" + mode).html("<center><img src='" + base_url + "/images/loading.gif'/></center>");
    $.post(base_url + "/ajax/ListProduct.aspx?search=" + mode + "&name=" + name + "&supplier=" + supplier
        + "&pfrom=" + pfrom + "&pto=" + pto, function(data){
        $("#content" + mode).html(data);
        $($("#content" + mode)[0]).css('display', 'none');
        $($("#content" + mode)[0]).fadeIn();
        tabArray[mode] = false;
    });
}
function clearSearchForm(id)
{
    $('#txtname' + id).val("");
    $('#ddlbrand' + id).val("0");
    $('#ddlpricefrom' + id).val("0");
    $('#ddlpriceto' + id).val("0");
}